<?php
/**
 * Created by PhpStorm.
 * User: ulinix
 * Date: 2017/10/28
 * Time: 12:31
 */
namespace Admin\Controller;
class Admin2017Controller extends CheckController{
    //pid ha asasn hawar qikirix
    public static function admin_news($pid="",$table1="",$table2="",$where="",$fields="",$l1="",$l2=""){
        //获取所有子分类ID hamma kiqik sahipingi ID siha erixidu
        if($pid){
            $catids=self::getAllChildcateIds($pid);
            $row=M("article")
                ->join($table1." ON "."$table2".'.pid='.$table1.'.id')
                ->where($table2.".pid IN ($catids) " .$where)
                ->field($fields)
                ->limit($l1,$l2)
                ->order("id desc")
                ->select();
        }else{
            $row=M("article")
                ->join($table1." ON "."$table2".'.pid='.$table1.'.id')
                ->where($where)
                ->field($fields)
                ->limit($l1,$l2)
                ->order("id desc")
                ->select();
        }

        return $row;
    }
    // //pid ha asasn hawar qikirix
    public static function getAllChildcateIds($categoryID)
    {
        //初始化ID数组
        $array[] = $categoryID;
        do{
            $ids = '';
            $cate = M('cate')->where("pid IN($categoryID)")->select();
            foreach ($cate as $k=>$v)
            {
                $array[] = $v['id'];
                $ids .= ',' . $v['id'];
            }
            $ids = substr($ids, 1, strlen($ids));
            $categoryID = $ids;
        }
        while (!empty($cate));
        $ids = implode(',', $array);
        return $ids;    //  返回字符串
        //return $array //返回数组
    }
}